package io.kiki.sba.registry.server.metadata.resource;

import io.kiki.sba.registry.common.model.GenericResponse;
import io.kiki.sba.registry.common.model.Response;
import io.kiki.sba.registry.common.model.metaserver.MultiClusterSyncInfo;
import io.kiki.sba.registry.common.model.metaserver.RemoteDatumClearEvent;
import io.kiki.sba.registry.server.metadata.remoting.data.DefaultDataServerService;
import io.kiki.sba.registry.server.shared.resource.AuthChecker;
import io.kiki.sba.registry.store.api.meta.MultiClusterSyncRepository;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;


@Path("api/multi/datum")
@Produces(MediaType.APPLICATION_JSON)
public class MultiDatumResource {

    private static final Logger logger = LoggerFactory.getLogger(MultiDatumResource.class);

    @Autowired
    private DefaultDataServerService defaultDataServerService;

    @Autowired
    private MultiClusterSyncRepository multiClusterSyncRepository;

    @POST
    @Path("/dataInfoId/removePubs")
    public Response clearDatumPublish(@FormParam("remoteDataCenter") String remoteDataCenter, @FormParam("dataInfoId") String dataInfoId, @FormParam("token") String token) {

        if (!AuthChecker.authCheck(token)) {
            logger.error("clear datum, remoteDataCenter={}, dataInfoId={}, auth check={} fail!", remoteDataCenter, dataInfoId, token);
            return GenericResponse.buildFailedResponse("auth check fail");
        }

        if (StringUtils.isBlank(remoteDataCenter) || StringUtils.isBlank(dataInfoId)) {
            return Response.buildFailedResponse("remoteDataCenter, dataInfoId is not allow empty.");
        }

        MultiClusterSyncInfo query = multiClusterSyncRepository.query(remoteDataCenter);
        if (query != null && query.isEnableSyncDatum()) {
            logger.error("clear datum forbidden when sync enable, remoteDataCenter:{}", remoteDataCenter);
            return Response.buildFailedResponse("clear datum forbidden when sync enable.");
        }

        defaultDataServerService.notifyRemoteDatumClear(RemoteDatumClearEvent.dataInfoIdEvent(remoteDataCenter, dataInfoId));
        return Response.buildSuccessResponse();
    }

    @POST
    @Path("/group/removePubs")
    public Response clearGroupDatumPublish(@FormParam("remoteDataCenter") String remoteDataCenter, @FormParam("group") String group, @FormParam("token") String token) {

        if (!AuthChecker.authCheck(token)) {
            logger.error("clear datum, remoteDataCenter={}, group={}, auth check={} fail!", remoteDataCenter, group, token);
            return GenericResponse.buildFailedResponse("auth check fail");
        }

        if (StringUtils.isBlank(remoteDataCenter) || StringUtils.isBlank(group)) {
            return Response.buildFailedResponse("remoteDataCenter, group is not allow empty.");
        }

        MultiClusterSyncInfo query = multiClusterSyncRepository.query(remoteDataCenter);
        if (query != null && query.isEnableSyncDatum()) {
            logger.error("clear datum forbidden when sync enable, remoteDataCenter:{}", remoteDataCenter);
            return Response.buildFailedResponse("clear datum forbidden when sync enable.");
        }

        defaultDataServerService.notifyRemoteDatumClear(RemoteDatumClearEvent.groupEvent(remoteDataCenter, group));
        return Response.buildSuccessResponse();
    }
}
